package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.File;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.CBRDModel;
import pl.edu.pw.MidEmu.Common.Models.MidasModel;
import pl.edu.pw.MidEmu.Logic.ProcessHandler;
import pl.edu.pw.MidEmu.UI.Controllers.CBRDLoaderController;
import pl.edu.pw.MidEmu.UI.Views.CBRDLoaderView;

/**
 * Frame with GUI for loading CBRD component
 * @author Mateusz Starodab
 *
 */
public class CBRDLoaderFrame extends BaseFrame<JDialog, CBRDLoaderController> implements CBRDLoaderView
{
	private JButton btnClose;
	private JButton btnPickCBRDBinDirLocation;
	private JButton btnStart;
	private JFileChooser fchChooseCBDRBinDirLocation;
	private JLabel lblCBRDBinDirLocation;
	private JLabel lblExecutableFileName;

	MidasComponent parent;
	private JPanel pnlButtons;

	private JPanel pnlCBRDBinDirLocation;

	private JPanel pnlCBRDBinDirLocation1;
	private JPanel pnlCBRDBinDirLocation2;

	private JPanel pnlContent;
	private JPanel pnlInnerContent;

	private JTextField tbxCBRDBinDirLocation;

	private JTextField tbxExecutableFileName;

	public CBRDLoaderFrame(MidasComponent midasComponent)
	{
		this.parent = midasComponent;

		CBRDLoaderController controller = new CBRDLoaderController();
		controller.setView(this);
		setController(controller);
		JDialog frame = new JDialog();
		setComponent(frame);

		getComponent().setTitle(Consts.MainFrame_Title + " - " + Consts.CBRDLoader_Title);
		getComponent().setSize(380, 230);
		getComponent().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		getComponent().setModal(true);

		pnlContent = new JPanel();
		pnlContent.setLayout(new BorderLayout());

		pnlInnerContent = new JPanel();
		pnlInnerContent.setBorder(BorderFactory.createTitledBorder(Consts.CBRDLoader_Caption));
		pnlInnerContent.setLayout(new FlowLayout(FlowLayout.LEFT));

		pnlCBRDBinDirLocation = new JPanel(new BorderLayout());
		pnlCBRDBinDirLocation1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		pnlCBRDBinDirLocation2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		lblCBRDBinDirLocation = new JLabel(Consts.CBDRLoader_CBDRBinDirLocation);
		tbxCBRDBinDirLocation = new JTextField(30);
		lblCBRDBinDirLocation.setLabelFor(pnlCBRDBinDirLocation);
		btnPickCBRDBinDirLocation = new JButton(Consts.CBRDLoader_CBRDBinDirLocationButton);
		btnPickCBRDBinDirLocation.setName(Consts.CBRDLoader_CBRDBinDirLocationButton);
		btnPickCBRDBinDirLocation.addActionListener(getController());

		pnlCBRDBinDirLocation1.add(tbxCBRDBinDirLocation);
		pnlCBRDBinDirLocation2.add(btnPickCBRDBinDirLocation);

		pnlCBRDBinDirLocation.add(pnlCBRDBinDirLocation1, BorderLayout.CENTER);
		pnlCBRDBinDirLocation.add(pnlCBRDBinDirLocation2, BorderLayout.SOUTH);

		pnlInnerContent.add(lblCBRDBinDirLocation);
		pnlInnerContent.add(pnlCBRDBinDirLocation);

		lblExecutableFileName = new JLabel(Consts.CBRDLoader_ExecutableFileNameLabel);
		tbxExecutableFileName = new JTextField(30);
		lblExecutableFileName.setLabelFor(tbxExecutableFileName);

		pnlInnerContent.add(lblExecutableFileName);
		pnlInnerContent.add(tbxExecutableFileName);

		pnlContent.add(pnlInnerContent, BorderLayout.CENTER);

		pnlButtons = new JPanel(new FlowLayout(FlowLayout.CENTER));

		btnStart = new JButton(Consts.CBRDLoader_StartButton);
		btnStart.setName(Consts.CBRDLoader_StartButton);
		btnStart.addActionListener(getController());
		pnlButtons.add(btnStart);

		btnClose = new JButton(Consts.CBRDLoader_CloseButton);
		btnClose.setName(Consts.CBRDLoader_CloseButton);
		btnClose.addActionListener(getController());
		pnlButtons.add(btnClose);

		pnlContent.add(pnlButtons, BorderLayout.SOUTH);

		getComponent().setContentPane(pnlContent);

		fchChooseCBDRBinDirLocation = new JFileChooser();
		fchChooseCBDRBinDirLocation.setMultiSelectionEnabled(false);
		fchChooseCBDRBinDirLocation.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		CBRDModel model = CBRDModel.load();
		if (model != null)
		{
			tbxCBRDBinDirLocation.setText(model.getCBRDWorkDirLocation());
			tbxExecutableFileName.setText(model.getExecutableFileName());
		}

		getComponent().getRootPane().setDefaultButton(btnStart);
	}

	@Override
	public void dispose()
	{
		getComponent().setVisible(false);
		getComponent().dispose();
	}

	@Override
	public String getBinDir()
	{
		return tbxCBRDBinDirLocation.getText();
	}

	@Override
	public String getCBRid()
	{
		return parent.getController().getMidas().getNodeName();
	}

	@Override
	public String getCDRBExecutableFileName()
	{
		return tbxExecutableFileName.getText();
	}

	@Override
	public MidasComponent getCentralNodeModel()
	{
		return parent.getCentralNodeModel();
	}

	@Override
	public MidasModel getParentMidasModel()
	{
		return parent.getController().getMidas();
	}

	@Override
	public void registerCBRDServer(CBRDModel model, ProcessHandler handler)
	{
		parent.getController().registerCBRD(model, handler);
	}

	public void reportValidationErrors()
	{
		JOptionPane.showMessageDialog(getComponent(), "CBR Demon configuration is invalid.\nMake sure you fill all fields", "Invalid configuration", JOptionPane.ERROR_MESSAGE);
	}

	@Override
	public void showPickupBinDirDialog()
	{
		fchChooseCBDRBinDirLocation.showOpenDialog(getComponent());
		File selectedFile = fchChooseCBDRBinDirLocation.getSelectedFile();
		if (selectedFile != null)
			tbxCBRDBinDirLocation.setText(selectedFile.getAbsolutePath());
	}

	public boolean validateCBRD()
	{
		if (getCBRid().equals(""))
			return false;
		if (getBinDir().equals(""))
			return false;
		if (getCDRBExecutableFileName().equals(""))
			return false;
		return true;
	}

	@Override
	public Hashtable<String, String> getNodesIDsAndIPs()
	{
		return parent.getNodesIDsAndIPs();
	}

	@Override
	public Hashtable<String, String> getNodesInterfacesAndIPS()
	{
		return parent.getNodesInterfacesAndIPS();
	}
}
